' This file is part of the SunDLL.  The SunDLL is free software: you can
' redistribute it and/or modify it under the terms of the GNU General Public
' License as published by the Free Software Foundation, version 2.
'
' This program is distributed in the hope that it will be useful, but WITHOUT
' ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
' FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
' details.
'
' You should have received a copy of the GNU General Public License along with
' this program; if not, write to the Free Software Foundation, Inc., 51
' Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'
' Copyright David R. Pratten

'Function D2dmsText(ByVal DTd As Ext )As String
'Dim d As Ext
'Dim m As Ext
'Dim rest  As Ext
'Dim s As Ext
'd = Fix(DTd)
'rest = Abs(DTd - d)
'rest = rest * 60
'm = Int(rest)
's = (rest - m) * 60
'Function = Format$(d,"&") + ":" + Format$(m,"00") + ":" + Format$(s,"00.00000")
'End Function
'FUNCTION EXT dms2d(tDegreeInt dint, tArcMinuteInt minint , tArcSecond second )
'FUNCTION = ((dint * 3600.) + (minint * 60.) + second) / 3600.
'END FUNCTION

Function d2Hour(ByVal dd As Ext ) As Ext
Function = dd / 15
End Function

 Function Hour2Min(ByVal h As Ext) As Ext
Function = h * 60
End Function
'
'
' ########################
' HMS2Unit ()
' ########################
'
Function HMS2Unit(ByVal Hour As Ext, ByVal Minute As Ext, ByVal Sec As Ext, ByVal ToUnit As Long) As Ext
Dim X As Ext
X = (Hour+Minute/60.+Sec/3600.)
Select Case tounit
    Case %UnitHour
         Function = X
    Case %UnitDay
         Function = X/24.
    Case %UnitDegree
         Function = X*15.
    Case %UnitTMin
         Function = X*60.
    Case %UnitTSec
         Function = X*3600.
    Case %UnitArcMin
         Function = X*15.*60.
    Case %UnitArcSec
         Function = X*15.*3600.
    Case %UnitRadian
         Function = X*15*DEGTORAD()
    Case Else
        Function = %BadParameter
End Select
End Function
'
'
' ########################
' DMS2Unit ()
' ########################
'
Function DMS2Unit(ByVal Degree As Ext, ByVal Minute As Ext, ByVal Sec As Ext, ByVal ToUnit As Long) As Ext
Dim X As Ext
X = (Degree+Minute/60.+Sec/3600.)
Select Case tounit
    Case  %UnitHour
        Function = X/15.
    Case  %UnitDay
         Function = X/360.
    Case  %UnitDegree
         Function = X
    Case  %UnitArcMin
         Function = X*60.
    Case  %UnitArcSec
         Function = X*3600.
    Case  %UnitTMin
         Function = X*4.
    Case  %UnitTSec
         Function = X*240.
    Case  %UnitRadian
         Function = X*DEGTORAD
    Case Else
        Function = %BadParameter
End Select
End Function


'
'
' ########################
' DMS2ArcUnit ()
' ########################
'
Function DMS2ArcUnit(ByVal Degree As Ext, ByVal Minute As Ext, ByVal Sec As Ext, ByVal ToUnit As Long)  As Ext
Dim X As Ext
X = (Degree+Minute/60.+Sec/3600.)
Select Case TOUnit
    Case  %UnitDegree
         Function = X
    Case %UnitArcMin
         Function = X*60.
    Case  %UnitArcSec
         Function = X*3600.
    Case %UnitRadian
         Function = X*DEGTORAD
    Case Else
        Function = %BadParameter
End Select
End Function
'
'

Function Reduce360(ByVal Angle As Ext ) As Ext
Dim Intbit As Ext
Dim Fracbit As Ext
Dim xReduce360 As Ext
If (Angle >= 0) And (Angle < 360) Then
   Function = Angle
   Exit Function
End If
If (Angle < 0) And (Angle > -360) Then
   Function = Angle + 360.
   Exit Function
End If

Intbit = Int(Angle)
Fracbit = Angle - Intbit
xReduce360 = Intbit  Mod 360
If xReduce360 < 0 Then
    xReduce360 = xReduce360 + 360
End If
xReduce360 = xReduce360 + Fracbit
Function = xReduce360
End Function

Function Reduce180Signed(ByVal Angle As Ext ) As Ext
Function = Reduce360(Angle + 180) - 180
End Function

Function Reduce1365(ByVal SDY As Ext ) As Ext
Dim Intbit As Ext
Dim  Fracbit As Ext
Dim  RSDY  As Ext
Dim xReduce1365 As Ext

RSDY = SDY - 1

If (RSDY >= 0) And (RSDY < 365) Then
   Function = RSDY+1.0
   Exit Function
End If

If (RSDY < 0) And (RSDY > -365) Then
   Function = RSDY + 365. +1.
   Exit Function
End If


Intbit = Int(RSDY)
Fracbit = RSDY - Intbit
xReduce1365 = Intbit  Mod 365
If xReduce1365 < 0 Then
    xReduce1365 = xReduce1365 + 365.
End If
xReduce1365 = xReduce1365 + Fracbit
Function = xReduce1365+1.
End Function

Sub PartsOfD(ByVal Degree As Ext,ByRef Sign As Long, ByRef DegreeOfD As Long, ByRef ArcMinuteOfD As Long, ByRef ArcSecOfD As Double)

Dim F As Ext

Sign = Sgn(Degree)


F = Abs(Degree)

DegreeOfD = Int(F)

F = F - DegreeOfD

If F > 0.999999999 Then
   F = 0
   Incr DegreeOfD
End If

F = F*60
ArcMinuteOfD = Int(F)

F = F - ArcMinuteOfD
If F > .999999999 Then
   F = 0
   Incr ArcMinuteOfD
End If

ArcSecOfD = F*60 ' only accurate to 10 decimal places

End Sub


